module.exports = function($scope, $resource, $state, $modalInstance, $modal, obj){

	$scope.objs = [];
    console.log('票机列表');

    console.log(obj);

    //$resource('/api/as/tc/device/watchdevicelist', {}, {}).save({'view' : obj.place_code}, function (res) {
    $resource('/api/as/wc/device/list', {}, {}).save({'view' : obj.place_code}, function (res) {

    	console.log(res);

	    if(res.errcode !== 0)
        {
            alert(res.errmsg);
            return;
        }

        if(res.data.length == 0){

        	var para = {
	            'code' : obj.place_code,
	            'pas' : '000000',
	            'view' : obj.place_code,
	            'type' : 1,
	            'state' : 1,
	            'del_flg' : 0,
	            'auth_state' : 0,
	            'many_state' : 1,
	            'group_auth_state' : 1,
	            'remarks' : '默认创建',
	        };

	        console.log(para);

			//$resource('/api/as/tc/device/create', {}, {}).save(para, function (res1) {
			$resource('/api/as/wc/device/create', {}, {}).save(para, function (res1) {
				console.log(res1);

				if (res.errcode !== 0) {
					alert(res.errmsg);
					return;
				}
				$scope.objs.push({'code' : obj.place_code, 'remarks' : '默认创建'});
			});

        }else{
        	$scope.objs = res.data;
        }

    });


    $scope.check = function(device){

    	console.log(device);

    	//$resource('/tktapi/sc/queryService/byCode', {}, {}).save({ "code": obj.code, "device": device.code }, function (res) {
    	$resource('/wdapi/sc/queryService/byCode', {}, {}).save({ "code": obj.code, "device": device.code }, function (res) {
			console.log(res);

			if (res.errcode !== 0) {
				alert(res.errmsg);
				return;
			}

			if(!confirm("您确认要核销这个票码吗？")) {
              	return;
          	}

			var p = {
				'code' : obj.code,
				'device' : device.code,
				'num' : 1,
				'reqkey' : res.data.reqkey,
			};

			//$resource('/tktapi/sc/destoryService/updateByCode', {}, {}).save(p, function (res1) {
			$resource('/wdapi/sc/destoryService/updateByCode', {}, {}).save(p, function (res1) {
				console.log(res1);

				if (res1.errcode !== 0) {
					alert(res1.errmsg);
					return;
				}

				$modalInstance.close();

			});

		});
    };

    

  	$scope.cancel = function(){

  		$modalInstance.close();

  	}

};
